﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class PlantBreadCrumb : Literal
    {
        private Queue crumbs = new Queue();
        private HtmlAnchor anchor = null;
        private int productId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProductId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);

        protected override void CreateChildControls()
        {
            crumbs.Enqueue(GetAnchor("花木列表首页", SiteUrls.Instance().GetProductListDefaultUrl()));

            Product product = BusinessManager.GetProduct(productId);
            if (categoryId == 0)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value+"_科别管理", SiteUrls.Instance().GetProductEditProductKebieUrl(productId)));
            }
            else if (categoryId == 1)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_名称科属版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId,categoryType,1)));
            }
            else if (categoryId == 2)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_分布习性版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 2)));
            }
            else if (categoryId == 3)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_形态特征版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 3)));
            }
            else if (categoryId == 4)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_适应环境版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 4)));
            }
            else if (categoryId == 5)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_栽培品种版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 5)));
            }
            else if (categoryId == 6)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_培育管理版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 6)));
            }
            else if (categoryId == 7)
            {
                crumbs.Enqueue(GetAnchor(product.Title.Value + "_主要用途版块管理", SiteUrls.Instance().GetPlantSection_SectionListUrl(productId, categoryType, 7)));
            }

        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("花木导航:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(" &#187; ");
                }
            }

        }

    }
}
